/**
 * meilele.com Inc.
 * Copyright (c) 2004-2016 All Rights Reserved.
 */
package com.gpagers.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import com.gpagers.core.ObjectRef;
import com.gpagers.core.UuidUtils;
import com.gpagers.file.FileSDK;

/**
 * 
 * @author liuzijie@meilele.com
 * @version $Id: MultiTaskGUI.java, v 0.1 2016年5月9日 上午11:20:29 liuzijie@meilele.com Exp $
 */
public class MultiTaskGUI extends Thread {

    private Shell shell = null;
    private Table table = null;

    /**
     * 
     */
    public MultiTaskGUI() {
        shell = ObjectRef.mainShell;
        //init menu
        //Menus m = new Menus();
        init();
        this.start();
        //shell.setMenuBar(m.init(shell));
    }

    public void init() {
        shell.setImage(new Image(ObjectRef.display, "images/alarm_clock_128px_1200611_easyicon.net.ico"));
        shell.setLayout(new GridLayout());
        shell.setText("Ginvoder");

        Button bt = new Button(shell, SWT.NONE);
        bt.setImage(new Image(ObjectRef.display, "images/addTask.png"));
        bt.setText("新建任务");

        table = new Table(shell, SWT.NONE);
        ObjectRef.table = table;
        table.setBackgroundMode(SWT.INHERIT_DEFAULT);
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        table.setHeaderVisible(true);
        table.setLinesVisible(true);
        String[] header = new String[] { "任务名字", "状态", "表达式", "操作", "删除" };
        for (int i = 0; i < header.length; i++) {
            TableColumn col = new TableColumn(table, SWT.CENTER);
            col.setText(header[i]);
        }

        table.getColumn(0).setWidth(230);
        table.getColumn(1).setWidth(80);
        table.getColumn(2).setWidth(300);
        table.getColumn(3).setWidth(80);
        table.getColumn(4).setWidth(80);
        shell.pack();
        bt.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                addNewTask();
            }
        });
        for (String data : FileSDK.readFileByLines()) {
            Task task = new Task(table);
            String name = data.split("\\|")[0];
            String expression = data.split("\\|")[1];
            String state = data.split("\\|")[2];
            task.createTableItem(name, expression, state);
        }

    }

    private void addNewTask() {
        Task task = new Task(table);
        task.popAddTaskUI(-1, UuidUtils.getUUID().substring(0, 7), "0/3 * * * * ?");
    }

    public Shell getShell() {
        return shell;
    }

    public void setShell(Shell shell) {
        this.shell = shell;
    }

    public Table getTable() {
        return table;
    }

    public void setTable(Table table) {
        this.table = table;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }

            if (ObjectRef.mainShell.isDisposed()) {
                return;
            }
            ObjectRef.display.asyncExec(new Runnable() {
                @Override
                public void run() {
                    if (ObjectRef.isMinAtCampo == true) {
                        if (ObjectRef.trayItem.getImage().equals(ObjectRef.g1Image)) {
                            ObjectRef.trayItem.setImage(ObjectRef.g2Image);
                        } else {
                            ObjectRef.trayItem.setImage(ObjectRef.g1Image);
                        }
                    }
                }
            });
        }
    }
}
